package com.nobrain.uboard.act;

import com.nobrain.uboard.fragment.CreateAccountFragment;
import com.nobrain.uboard.fragment.MainAct_Services;
import com.nobrain.uboard.fragment.Timeline;

import android.app.ActionBar;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;

public class MainAct extends FragmentActivity {
	public final static boolean isHoneyComb = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
	
	private MainAct_Services.OnItemChangeListener l;
	
	public void setOnItemChangeListener(MainAct_Services.OnItemChangeListener l) {
		this.l = l;
	}
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.main);
		initView();
	}
	void initView(){
		if(isHoneyComb){
			ActionBar mActionBar = getActionBar();
			mActionBar.setTitle(R.string.app_name);
		}else{
			setTitle(R.string.app_name);
		}
		
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		
		ft.add(R.id.fl_services, new MainAct_Services());
		ft.add(R.id.fl_timeline, new Timeline());
		ft.commit();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_editmode, menu);
		
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case R.id.menu_main_plus:
			FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
			Fragment prev = getSupportFragmentManager().findFragmentByTag("sns_add_category");
			if(prev != null){
				ft.remove(prev);
			}
			ft.addToBackStack(null);
			
			CreateAccountFragment dailogFragment = new CreateAccountFragment();
			dailogFragment.show(ft, "sns_add_category");
			break;
		case R.id.menu_main_minus:
			l.delItem(0);
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
